package com.app.controller;

import com.app.common.Result;
import com.app.common.SystemConstant;
import com.app.model.ArticleModel;
import com.app.server.ArticleService;
import com.app.utils.CommonHelp;
import com.app.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/sbbz/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 列表
     *
     * @param page  第页
     * @param limit 限制
     * @return {@link Result}
     */
    @GetMapping("/list")
    public Result articleList(Integer page, Integer limit, String keyword) {
        ArticleVo vo = articleService.selectArticleList(page, limit, keyword);
        return Result.success(vo);
    }

    /**
     * 新增和编辑
     *
     * @param article 轮播图
     * @return {@link Result}
     */
    @PostMapping("/save")
    public Result articleSave(@RequestBody @Valid ArticleModel article, HttpServletRequest request) {
        if (article.getId() != null && article.getId() > 0) {
            article.setUpdateTime(CommonHelp.getNowTimestamp());
            return Result.success(this.articleService.updateById(article));
        } else {
            article.setStatus(1);
            article.setCreateTime(CommonHelp.getNowTimestamp());
            return Result.success(this.articleService.save(article));
        }
    }

    /**
     * 删除
     *
     * @return {@link Result}
     */
    @GetMapping("/delete")
    public Result articleDelete(Integer id) {
        return Result.success(this.articleService.removeById(id));
    }

    /**
     * 详细信息
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/details")
    public Result details(Integer id) {
        ArticleModel article = articleService.getById(id);
        return Result.success(article);
    }
}
